//-----------------------------------------------------------------------------
// DeleteSubscriber
//
// Copyright (c) MetaSwitch 2006
// Author: Alex Davies
//
// Sample application demonstrating the SOAP Sh interface to the MetaView
// Server.  This application uses Sh-Pull to get a subscriber's current base
// settings, and then sends an Sh-Update with an instruction to delete the
// subscriber.  It uses Apache Axis and the "untyped" WSDL file.
//
// See the readme file for more information.
//-----------------------------------------------------------------------------

import javax.xml.rpc.holders.*;
import javax.xml.soap.SOAPException;

import org.apache.axis.message.MessageElement;

import com.MetaSwitch.EMS.SOAP.*;
import com.MetaSwitch.EMS.SOAP.holders.*;

public class DeleteSubscriber
{
  private static final String USAGE = "Usage: DeleteSubscriber " +
                                      "<Directory number>";

  private static ShUntypedUtilities utilities = new ShUntypedUtilities();

  public static void main(String[] args)
  {
    StringHolder dNHolder = new StringHolder();

    IntHolder resultCodeHolder = new IntHolder();
    TExtendedResultHolder extendedResultHolder = new TExtendedResultHolder();
    TUserDataHolder userDataHolder = new TUserDataHolder();

    try
    {
      //-----------------------------------------------------------------------
      // Extract information from the command line arguments.
      //-----------------------------------------------------------------------
      getParameters(args, dNHolder);

      //-----------------------------------------------------------------------
      // Connect to the Sh service and send in a Pull request.  Check that it
      // succeeds.
      //-----------------------------------------------------------------------
      ShServiceLocator locator = new ShServiceLocator();

      Sh shInstance = locator.getShService();

      shInstance.shPull(dNHolder.value,
                        0,
                        "Meta_Subscriber_BaseInformation",
                        utilities.ORIGIN_HOST,
                        resultCodeHolder,
                        extendedResultHolder,
                        userDataHolder);

      utilities.checkResultCode(resultCodeHolder.value,
                                extendedResultHolder.value,
                                userDataHolder.value);

      //-----------------------------------------------------------------------
      // Update the user data:
      // -  Update the sequence number.
      // -  Remove all the fields.
      // -  Add the action attribute to the field group element.
      //-----------------------------------------------------------------------
      utilities.incrementSequenceNumber(userDataHolder.value);

      MessageElement fieldGroup =
                          utilities.getFieldGroupElement(userDataHolder.value);

      fieldGroup.removeContents();
      fieldGroup.addAttribute("", "", "Action", "delete");

      //-----------------------------------------------------------------------
      // Make the update request and check it succeeds.
      //-----------------------------------------------------------------------
      shInstance.shUpdate(dNHolder.value,
                          0,
                          userDataHolder.value,
                          utilities.ORIGIN_HOST,
                          resultCodeHolder,
                          extendedResultHolder);

      utilities.checkResultCode(resultCodeHolder.value,
                                extendedResultHolder.value,
                                userDataHolder.value);

      System.out.println("Subscriber " + dNHolder.value +
                         " successfully deleted.");
    }
    catch (MetaSwitchShInterfaceException e)
    {
      System.err.println(e.getMessage());
      System.err.println(USAGE);
    }
    catch (org.apache.axis.AxisFault e)
    {
      System.err.println("Problem connecting to MetaView server: " + e);
      System.err.println(USAGE);
    }
    catch (javax.xml.rpc.ServiceException e)
    {
      System.err.println("Could not connect to Sh service");
      System.err.println(USAGE);
    }
    catch (Exception e)
    {
      System.err.println("Unexpected error \"" + e
                         + "\" in retrieving data");
      e.printStackTrace(System.err);
      System.err.println(USAGE);
    }
  }

  /**
   * Parse the command line arguments and extract the necessary information.
   *
   * The only argument is the subscriber's directory number, which is required.
   *
   * @param args        IN  The arguments provided at the command line.
   * @param dN          OUT The directory number of the subscriber to delete.
   *
   * @exception WrongParametersException
   */
  private static void getParameters(String[] args, StringHolder dN)
    throws WrongParametersException
  {
    if (args.length != 1)
    {
      throw new WrongParametersException("Wrong number of parameters.");
    }

    dN.value = args[0];
  }
}
